<?php
session_start();
    
    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // September 27, 2011
    // IN PARAMETERS : documentSetupId, docType
    // 
    // This AJAX Request identifies if a Document is not part of any active MainDocuments / Attachments.


if ($_POST) {
    include('../includes/siteConfig.php');
    
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    // GET VARIABLES VIA POST METHOD
    $documentSetupId = $_POST['documentSetupId'];
    $docType = $_POST['docType'];
    
    if ($docType == "Attachment") {
        //QUERY TO VERIFY IF A DOCUMENT CAN BE MODIFIED (ATTACHMENT).
         $query = 
            "
                SELECT *
                FROM RouteStep_Req_Attachment
                WHERE stepId IN (
                     SELECT stepId
                     FROM RouteStep
                     WHERE routeId IN (
                          SELECT DISTINCT (RouteStep.routeId)
                          FROM MainDocument, RouteStep
                          WHERE MainDocument.status = 'Active' AND 
                                MainDocument.currentStepId = RouteStep.stepId
                          )
                     ) AND documentSetupId = '$documentSetupId'
            ";
        
    }
     
    else {
        //QUERY TO VERIFY IF A DOCUMENT CAN BE MODIFIED (MAIN DOCUMENT).
         $query = 
            "
                SELECT *
                FROM MainDocument
                WHERE documentSetupId = '$documentSetupId' AND status= 'Active'
            ";
    }
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        $numRows = mysql_num_rows($result);
        
        // THIS MEANS THAT THE DOCUMENT HAS ON GOING OR FUTURE TRANSACTIONS.
        if (mysql_num_rows($result) > 0) {
            echo json_encode(1);
        }
        
        else {
            echo json_encode(0);
        }
   
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
